package com.pc.tas.learning.trace;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Collection;
import java.util.Date;

public class TraceService {
	private TraceDAO traceDao;
	private DataSource dataSource;
	private static TraceService instance;

	private static Log log = LogFactory.getLog(TraceService.class);

	public synchronized static TraceService getInstance() {
		if (instance == null) {
			instance = new TraceService();
		}
		return instance;
	}
	
	private TraceService() {
		//DataSource ds = (DataSource) SpringBeanUtil.getBean("datasource");
		//this.dataSource = ds;
		this.traceDao = new TraceDAO();
	}
	
	public int addUserTrace(Trace userTrace) throws Exception {
		Connection conn = null;
		try {
			conn = this.dataSource.getConnection();
			this.traceDao.add(conn, userTrace);
			return 0;
		} catch (Exception e) {
			log.error(e);
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}

	public void updateUserTrace(Trace userTrace,String id) throws Exception {
		Connection conn = null;
		try {
			conn = this.dataSource.getConnection();
			this.traceDao.update(conn, userTrace,id);
		} catch (Exception e) {
			log.error(e);
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}
	

	public void deleteUserTrace(int id) throws Exception {
		Connection conn = null;
		try {
			conn = this.dataSource.getConnection();
			this.traceDao.delete(conn, id);
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}

	public Trace getUserTraceById(String id) throws Exception {
		Connection conn = null;
		Trace userTrace = null;
		try {
			conn = this.dataSource.getConnection();
			userTrace = (Trace) this.traceDao.getById(conn, id);
		} catch (Exception e) {
			log.error(e);
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
		return userTrace;
	}
	
	public Collection<Trace> getPage(Date date, int start, int number) throws Exception {
		Connection conn = null;
		Collection<Trace> traces = null;
		try {
			conn = this.dataSource.getConnection();
			traces = this.traceDao.getPage(conn, date,start,number);
		} catch (Exception e) {
			log.error(e);
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
		return traces;
	}
	
	public Collection<Trace> getPage(String date, int start, int number) throws Exception {
		Connection conn = null;
		Collection<Trace> traces = null;
		try {
			conn = this.dataSource.getConnection();
			traces = this.traceDao.getPage(conn, date,start,number);
		} catch (Exception e) {
			log.error(e);
			throw e;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
		return traces;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
}
